package com.df.my_net_game.net.tcp;

import com.df.my_net_game.net.base.GTcpIdleHandler;
import com.df.my_net_game.net.base.GTcpServer;
import com.df.my_net_game.net.base.IServerCB;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

public class EzGameTcpDefatultChannelInitializer extends ChannelInitializer<SocketChannel> {
    GTcpServer server;

    public EzGameTcpDefatultChannelInitializer(GTcpServer server) {
        this.server = server;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 增加心跳支持 start
        // 针对客户端, 如果在 1 分钟之内没有向服务端发送心跳(All),则主动断开
        // 如果是读空闲或者写空闲, 则不处理
        pipeline.addLast(new IdleStateHandler(server.getReaderIdleTimeSecond(), server.getReaderIdleTimeSecond(), server.getAllIdleTimeSecond()));
        // 自定义的空闲状态检测
        pipeline.addLast(new GTcpIdleHandler(server));

        //协议
        IServerCB serverCB = server.getServerCB();
        if(serverCB!=null){
            serverCB.onChannelInit(pipeline,server);
        }
    }
}
